Skip to content

TYP: _config/config.py && core/{apply,construction}.py #30734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 16, 2020

Conversation

ShaharNaveh
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Jan 6, 2020
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MomIsBestFriend Thanks for the PR. generally lgtm ex Generics without type parameters, see #30539

ideally we want that list to shrink!

@@ -211,7 +211,7 @@ def __getattr__(self, key: str):
else:
return _get_option(prefix)

def __dir__(self):
def __dir__(self) -> Iterable[str]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __dir__(self) -> Iterable[str]:
def __dir__(self) -> List[str]:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely recall this coming up elsewhere. Actually looks like this is inconsistent in typeshed though stdlib matches what's here:

https://github.com/python/typeshed/blob/a1331accbe98ae5f214b824121e5223e52a474f7/stdlib/2and3/builtins.pyi#L67

No strong preference on either, just adding for visibility

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely recall this coming up elsewhere.

I got the same feeling while annotating this.
So I looked hard in my previous PR until I found it.
#30411 (comment)

@@ -818,7 +823,7 @@ def inner(x):
return inner


def is_nonnegative_int(value):
def is_nonnegative_int(value: Optional[int]) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange that this returns None. I would expect a function named is_* to return True/False.

Copy link
Member

@WillAyd WillAyd Jan 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think OK to rename this check_nonnegative_int in another PR

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm outside of @simonjayhawkins comments

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MomIsBestFriend a few suggestions otherwise lgtm.

key: str,
defval: object,
doc: str = "",
validator: Optional[Union[Callable[..., None], Type[str]]] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Type[str] shouldn't be needed. changing the Callable to allow it to return a value fixes this. Also the validator should accept only a single argument.

Suggested change
validator: Optional[Union[Callable[..., None], Type[str]]] = None,
validator: Optional[Callable[[Any], Any]] = None,

@@ -729,7 +734,7 @@ def config_prefix(prefix):
global register_option, get_option, set_option, reset_option

def wrap(func):
def inner(key, *args, **kwds):
def inner(key: str, *args, **kwds) -> Callable[[str], Any]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be annotated using the pattern for decorators i.e annotate the outer function using a typevar and cast the return type of inner function, see https://mypy.readthedocs.io/en/latest/generics.html#declaring-decorators

no need to annotate the inner function

defval: object,
doc: str = "",
validator: Optional[Union[Callable[..., None], Type[str]]] = None,
cb: Optional[Callable[[str], None]] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to validator, restricting the return type to None may not be desirable.

@@ -751,7 +756,7 @@ def inner(key, *args, **kwds):
# arg in register_option


def is_type_factory(_type):
def is_type_factory(_type) -> Callable[..., None]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the returned function accepts exactly one argument

Suggested change
def is_type_factory(_type) -> Callable[..., None]:
def is_type_factory(_type: Type[Any]) -> Callable[[Any], None]:

if type(x) != _type:
raise ValueError(f"Value must have type '{_type}'")

return inner


def is_instance_factory(_type):
def is_instance_factory(_type) -> Callable[..., None]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the returned function accepts exactly one argument

Suggested change
def is_instance_factory(_type) -> Callable[..., None]:
def is_instance_factory(_type) -> Callable[[Any], None]:

if not isinstance(x, _type):
raise ValueError(f"Value must be an instance of {type_repr}")

return inner


def is_one_of_factory(legal_values):
def is_one_of_factory(legal_values) -> Callable[..., None]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def is_one_of_factory(legal_values) -> Callable[..., None]:
def is_one_of_factory(legal_values) -> Callable[[Any] None]:

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MomIsBestFriend lgtm pending green. @WillAyd

@simonjayhawkins simonjayhawkins added this to the 1.1 milestone Jan 12, 2020
@WillAyd WillAyd merged commit 171a1ed into pandas-dev:master Jan 16, 2020
@WillAyd
Copy link
Member

WillAyd commented Jan 16, 2020

Thanks @MomIsBestFriend

@ShaharNaveh ShaharNaveh deleted the TYP-_config/core branch January 16, 2020 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants